{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Transfer Attack on CIFAR10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "\n",
    "sys.path.insert(0, '..')\n",
    "import torchattacks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load source model and data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Files already downloaded and verified\n",
      "[Data loaded]\n",
      "[Model loaded]\n",
      "Acc: 100.00 %\n"
     ]
    }
   ],
   "source": [
    "sys.path.insert(0, '..')\n",
    "import robustbench\n",
    "from robustbench.data import load_cifar10\n",
    "from robustbench.utils import load_model, clean_accuracy\n",
    "\n",
    "images, labels = load_cifar10(n_examples=5)\n",
    "print('[Data loaded]')\n",
    "\n",
    "device = \"cuda\"\n",
    "model = load_model('Wong2020Fast', norm='Linf').to(device)\n",
    "acc = clean_accuracy(model, images.to(device), labels.to(device))\n",
    "print('[Model loaded]')\n",
    "print('Acc: %2.2f %%'%(acc*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "- Save progress: 100.00 % / Robust accuracy: 60.00 % / L2: 1.64071 (0.180 it/s) \t\n"
     ]
    }
   ],
   "source": [
    "from torchattacks import PGD\n",
    "atk = PGD(model, eps=8/255, alpha=2/225, steps=10, random_start=True)\n",
    "atk.save(data_loader=[(images, labels)], save_path=\"_transfer.pt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load target model and adv data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data is loaded in the following order: [adv_inputs, labels]\n"
     ]
    }
   ],
   "source": [
    "adv_loader = atk.load(load_path=\"_transfer.pt\")\n",
    "adv_images, labels = iter(adv_loader).next()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Model loaded]\n",
      "Acc: 60.00 %\n"
     ]
    }
   ],
   "source": [
    "model = load_model('Standard', norm='Linf').to(device)\n",
    "print('[Model loaded]')\n",
    "acc = clean_accuracy(model, adv_images.to(device), labels.to(device))\n",
    "print('Acc: %2.2f %%'%(acc*100))"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "1ceb8aea646a0c712ed5db194d127de24ece80f87032283552cbe7de982c3798"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
